package org.bonorsoft.modules.mobileutils.login;

import org.apache.shiro.SecurityUtils;
import org.bonorsoft.common.system.vo.LoginUser;
import org.bonorsoft.common.util.PasswordUtil;
import org.bonorsoft.modules.mobile.entity.MobileUser;
import org.bonorsoft.modules.mobile.service.IMobileUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
public class MobileLoginInfo {
    @Autowired
    private IMobileUserService mobileUserService;
    private static IMobileUserService userService;

    @PostConstruct
    public void init() {
        userService = mobileUserService;
    }
    public static LoginUser getLoginUser(){
        return (LoginUser) SecurityUtils.getSubject().getPrincipal();
    }
    public static boolean matches(String password) {
        LoginUser user = getLoginUser();
        MobileUser mobileUser = userService.getById(user.getId());
        String userpassword = PasswordUtil.encrypt(user.getUsername(), password, mobileUser.getSalt());
        String syspassword = mobileUser.getPassword();
        return syspassword.equals(userpassword);
    }
}
